package com.aiyige.page.my.localVideo.menuListview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.aiyige.R;


public class MenuListItem extends LinearLayout {
	private SwipeLayout swipeLayout;
	private LinearLayout contentLayout;
	private View editView;
	private View deleteView;
	
	private MenuListener menuListener;
	private boolean showEdit;
	

	public MenuListItem(Context context, boolean showEdit) {
		super(context);
		this.showEdit = showEdit;
		init(context);
	}


	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.menu_list_item, this);
		swipeLayout = (SwipeLayout) findViewById(R.id.swipe);
		contentLayout = (LinearLayout) findViewById(R.id.ll_content);
		editView = findViewById(R.id.edit);
		deleteView = findViewById(R.id.delete);
		
		editView.setVisibility(showEdit ? View.VISIBLE : View.GONE);
		
		editView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != menuListener) {
					menuListener.onEdit();
				}
				swipeLayout.close();
			}
		});
		
		deleteView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != menuListener) {
					menuListener.onDelete();
				}
				swipeLayout.close();
			}
		});
		
	}
	
	public void setContentView(View v) {
		contentLayout.addView(v, new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
	}

	public void setMenuWidth(int width) {
		LayoutParams editLp = (LayoutParams)editView.getLayoutParams();
		editLp.width = width;
		editView.setLayoutParams(editLp);

		LayoutParams delLp = (LayoutParams)deleteView.getLayoutParams();
		delLp.width = width;
		deleteView.setLayoutParams(delLp);
	}
	
	public void setMenuListener(MenuListener listener) {
		menuListener = listener;
	}
	
	public void setSwipeEnable(boolean enable) {
		swipeLayout.setSwipeEnabled(enable);
	}
	
	/**
	 *  只显示编辑
	 */
	public void onlyShowEdit() {
		editView.setVisibility(View.VISIBLE);
		deleteView.setVisibility(View.GONE);
	}
	
	public interface MenuListener {
		public void onDelete();
		public void onEdit();
	}
}
